package cn.iocoder.an.module.business.controller.admin.equipmentcheckplan;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentcheckplan.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckplan.EquipmentCheckPlanDO;
import cn.iocoder.an.module.business.service.equipmentcheckplan.EquipmentCheckPlanService;

@Tag(name = "管理后台 - 点检计划")
@RestController
@RequestMapping("/business/equipment-check-plan")
@Validated
public class EquipmentCheckPlanController {

    @Resource
    private EquipmentCheckPlanService equipmentCheckPlanService;



    @PostMapping("/create")
    @Operation(summary = "创建点检计划")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-plan:create')")
    public CommonResult<Long> createEquipmentCheckPlan(@Valid @RequestBody EquipmentCheckPlanSaveReqVO createReqVO) {
        return success(equipmentCheckPlanService.createEquipmentCheckPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点检计划")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-plan:update')")
    public CommonResult<Boolean> updateEquipmentCheckPlan(@Valid @RequestBody EquipmentCheckPlanSaveReqVO updateReqVO) {
        equipmentCheckPlanService.updateEquipmentCheckPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除点检计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-check-plan:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckPlan(@RequestParam("id") Long id) {
        equipmentCheckPlanService.deleteEquipmentCheckPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除点检计划")
                @PreAuthorize("@ss.hasPermission('business:equipment-check-plan:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckPlanList(@RequestParam("ids") List<Long> ids) {
        equipmentCheckPlanService.deleteEquipmentCheckPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得点检计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-plan:query')")
    public CommonResult<EquipmentCheckPlanRespVO> getEquipmentCheckPlan(@RequestParam("id") Long id) {
        EquipmentCheckPlanDO equipmentCheckPlan = equipmentCheckPlanService.getEquipmentCheckPlan(id);
        return success(BeanUtils.toBean(equipmentCheckPlan, EquipmentCheckPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得点检计划分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-plan:query')")
    public CommonResult<PageResult<EquipmentCheckPlanRespVO>> getEquipmentCheckPlanPage(@Valid EquipmentCheckPlanPageReqVO pageReqVO) {
        PageResult<EquipmentCheckPlanDO> pageResult = equipmentCheckPlanService.getEquipmentCheckPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentCheckPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点检计划 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentCheckPlanExcel(@Valid EquipmentCheckPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentCheckPlanDO> list = equipmentCheckPlanService.getEquipmentCheckPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "点检计划.xls", "数据", EquipmentCheckPlanRespVO.class,
                        BeanUtils.toBean(list, EquipmentCheckPlanRespVO.class));
    }

}